package org.example.shixiguanli.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.example.base.common.model.UserModel;
import org.example.base.common.service.UserService;
import org.example.core.mysql.Where;
import org.example.shixiguanli.model.TPostModel;
import org.example.shixiguanli.model.TPracticeModel;
import org.example.shixiguanli.service.TPostService;
import org.example.shixiguanli.service.TPracticeService;
import org.example.shixiguanli.service.TStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2024/2/15 23:23
 */
@Service
public class TStatisticsServiceImpl implements TStatisticsService {

    @Autowired
    private TPracticeService tPracticeService;

    @Autowired
    private UserService userService;

    @Autowired
    private TPostService tPostService;

    @Override
    public JSONObject practiceCount() {
        JSONObject result = new JSONObject();
        String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        List<UserModel> userModelList = userService.search(Where.build().field("teacher_user_id").equal(userId).and().field("del_flag").equal(0));
        List<String> userIdList = userModelList.stream().map(UserModel::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(userIdList)) {
            result.set("userCount", 0);
            result.set("practiceCount", 0);
            return result;
        }
        List<TPracticeModel> practiceModelList = tPracticeService.search(Where.build().field("user_id").in(userIdList));
        result.set("userCount", userIdList.size());
        result.set("practiceCount", ObjectUtils.isEmpty(practiceModelList) ? 0 : practiceModelList.size());
        return result;
    }

    @Override
    public JSONArray everyStudentCount() {
        JSONArray result = new JSONArray();
        String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        List<UserModel> userModelList = userService.search(Where.build().field("teacher_user_id").equal(userId).and().field("del_flag").equal(0));
        userModelList.stream().forEach(userModel -> {
            JSONObject userJsonObject = JSONUtil.parseObj(userModel);
            int weekendLogCount = tPracticeService.count(Where.build().field("user_id").equal(userModel.getId()).and().field("type").equal(0).and().field("del_flag").equal(0));
            int practiceLogCount = tPracticeService.count(Where.build().field("user_id").equal(userModel.getId()).and().field("type").equal(1).and().field("del_flag").equal(0));
            userJsonObject.set("practiceLogCount", practiceLogCount);
            userJsonObject.set("weekendLogCount", weekendLogCount);
            result.add(userJsonObject);
        });
        return result;
    }

    @Override
    public Map<String, JSONObject> everyPostCount() {
        List<TPostModel> postModelList = tPostService.search(Where.build().field("del_flag").equal(0));
        String userId = ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        List<UserModel> userModelList = userService.search(Where.build().field("teacher_user_id").equal(userId).and().field("del_flag").equal(0));
        List<String> userIdList = userModelList.stream().map(UserModel::getId).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(userIdList)) {
            Map<String, JSONObject> postMap =
                    postModelList.stream().collect(Collectors.toMap(TPostModel::getId, tPostModel -> {
                        JSONObject postJsonObject = JSONUtil.parseObj(tPostModel);
                        postJsonObject.set("count", 0);
                        return postJsonObject;
                    }));
            return postMap;
        } else {

            Map<String, JSONObject> postMap =
                    postModelList.stream().collect(Collectors.toMap(TPostModel::getId, tPostModel -> {
                        JSONObject postJsonObject = JSONUtil.parseObj(tPostModel);
                        List<TPracticeModel> practiceModelList = tPracticeService.search(Where.build().field("user_id").in(userIdList));
                        String postId = tPostModel.getId();
                        AtomicInteger count = new AtomicInteger();
                        practiceModelList.stream().forEach(practiceModel -> {
                            if (postId.equals(practiceModel.getPostId())) {
                                count.getAndIncrement();
                            }
                        });
                        postJsonObject.set("count", count.get());
                        return postJsonObject;
                    }));
            return postMap;
        }
    }
}
